package com.brainhealth.web.controller.customer;

import com.alibaba.fastjson.JSONObject;
import com.brainhealth.common.annotation.Anonymous;
import com.brainhealth.common.annotation.Log;
import com.brainhealth.common.annotation.RepeatSubmit;
import com.brainhealth.common.core.controller.BaseController;
import com.brainhealth.common.core.domain.AjaxResult;
import com.brainhealth.common.core.page.TableDataInfo;
import com.brainhealth.common.enums.BusinessType;
import com.brainhealth.common.utils.SnowFlakeUtil;
import com.brainhealth.common.utils.StringUtils;
import com.brainhealth.common.utils.poi.ExcelUtil;
import com.brainhealth.customer.domain.Customer;
import com.brainhealth.customer.domain.ExerciseOrder;
import com.brainhealth.customer.domain.ExerciseOrderPay;
import com.brainhealth.customer.domain.vo.ExerciseOrderPayVO;
import com.brainhealth.customer.mapper.ExerciseOrderPayMapper;
import com.brainhealth.customer.service.IExerciseOrderService;
import com.brainhealth.utils.CommonUtils;
import com.huifu.adapay.core.AdapayCore;
import com.huifu.adapay.core.util.AdapaySign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 会员训练订单Controller
 * 
 * @author wolong
 * @date 2023-05-09
 */
@RestController
@RequestMapping("/customer/order")
public class ExerciseOrderController extends BaseController
{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IExerciseOrderService exerciseOrderService;

    /**
     * 查询会员训练订单列表
     */
    @PreAuthorize("@ss.hasPermi('customer:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExerciseOrder exerciseOrder)
    {
        startPage();
        List<ExerciseOrder> list = exerciseOrderService.selectExerciseOrderList(exerciseOrder);
        return getDataTable(list);
    }

    /**
     * 导出会员训练订单列表
     */
    @PreAuthorize("@ss.hasPermi('customer:order:export')")
    @Log(title = "会员训练订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExerciseOrder exerciseOrder)
    {
        List<ExerciseOrder> list = exerciseOrderService.selectExerciseOrderList(exerciseOrder);
        ExcelUtil<ExerciseOrder> util = new ExcelUtil<ExerciseOrder>(ExerciseOrder.class);
        util.exportExcel(response, list, "会员训练订单数据");
    }

    /**
     * 获取会员训练订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:order:query')")
    @GetMapping(value = "/{exerciseOrderId}")
    public AjaxResult getInfo(@PathVariable("exerciseOrderId") Long exerciseOrderId)
    {
        return success(exerciseOrderService.selectExerciseOrderByExerciseOrderId(exerciseOrderId));
    }

    /**
     * 新增会员训练订单
     */
    @PreAuthorize("@ss.hasPermi('customer:order:add')")
    @Log(title = "会员训练订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExerciseOrder exerciseOrder)
    {
        return toAjax(exerciseOrderService.insertExerciseOrder(exerciseOrder));
    }

    /**
     * 修改会员训练订单
     */
    @PreAuthorize("@ss.hasPermi('customer:order:edit')")
    @Log(title = "会员训练订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExerciseOrder exerciseOrder)
    {
        return toAjax(exerciseOrderService.updateExerciseOrder(exerciseOrder));
    }

    /**
     * 删除会员训练订单
     */
    @PreAuthorize("@ss.hasPermi('customer:order:remove')")
    @Log(title = "会员训练订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{exerciseOrderIds}")
    public AjaxResult remove(@PathVariable Long[] exerciseOrderIds)
    {
        return toAjax(exerciseOrderService.deleteExerciseOrderByExerciseOrderIds(exerciseOrderIds));
    }

    /**
     * 会员token获取会员订单列表
     */
    @Anonymous
    @PostMapping("/getCustomerOrderList")
    public TableDataInfo getCustomerOrderList(HttpServletRequest request)
    {
        startPage();
        List<ExerciseOrder> lstOrder = exerciseOrderService.getCustomerOrderList(request);
        return getDataTable(lstOrder);
    }

    /**
     * 训练产品订单支付回调
     */
    @Anonymous
    @RequestMapping("/payCallback")
    public void payCallback(HttpServletRequest request, HttpServletResponse response)
    {
        exerciseOrderService.customerExerciseOrderCallback(request, response);
    }

    /**
     * 创建训练订单
     * @param request
     * @param exerciseOrderPayVO
     * @return
     */
    @Anonymous
    @RepeatSubmit
    @PostMapping("/createExerciseOrder")
    public AjaxResult createExerciseOrder(HttpServletRequest request, @RequestBody ExerciseOrderPayVO exerciseOrderPayVO)
    {
        ExerciseOrder exerciseOrder = exerciseOrderService.createExerciseOrder(request, exerciseOrderPayVO);
        if (exerciseOrder != null)
        {
            return AjaxResult.success("脑常青产品下单成功！", exerciseOrder);
        }
        return AjaxResult.error("脑常青产品下单失败！");
    }

    /**
     * 训练产品订单支付
     */
    @Anonymous
    @PostMapping("/exerciseOrderPay")
    public AjaxResult exerciseOrderPay(HttpServletRequest request, @RequestBody ExerciseOrderPayVO exerciseOrderPayVO)
    {
        if (StringUtils.isNullOrEmpty(exerciseOrderPayVO.getOrderId()))
        {
            throw new RuntimeException("订单号不能为空！");
        }
        ExerciseOrder exerciseOrder = exerciseOrderService.customerExerciseOrderPay(request, exerciseOrderPayVO);
        if (exerciseOrder != null)
        {
            return AjaxResult.success("脑常青产品订单支付成功！", exerciseOrder);
        }
        return AjaxResult.error("脑常青产品订单支付失败！");
    }

    /**
     * 用训练订单ID获取
     */
    @Anonymous
    @PostMapping("/getExerciserOrderById")
    public AjaxResult getExerciserOrderById(@RequestBody Map<String, Object> map)
    {
        if (map.get("orderId") == null)
        {
            throw new RuntimeException("订单号不能为空！");
        }
        String orderId = map.get("orderId").toString();
        return AjaxResult.success(exerciseOrderService.selectExerciseOrderByOrderId(orderId));
    }

    /**
     * 查询用户是否存在待支付订单的倒计时时间
     * @param request
     * @return
     */
    @Anonymous
    @PostMapping("/exerciseOrderRemainTime")
    public AjaxResult exerciseOrderRemainTime(HttpServletRequest request)
    {
        Date orderTime = exerciseOrderService.exerciseOrderRemainTime(request);
        if (orderTime != null)
        {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return AjaxResult.success("用户待支付订单剩余时间", sdf.format(orderTime));
        }
        return AjaxResult.error("用户没有待支付的订单！");
    }
}
